package com.open.party.cloud.view.home.juBao

import android.os.Bundle
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentPagerAdapter
import com.jiangyy.easydialog.CommonDialog
import cn.android.x.model.data.ResultInfo
import cn.android.x.parent.AppEventBusTitleBaseActivity
import com.open.party.cloud.R
import com.open.party.cloud.model.JuBaoBean
import com.open.party.cloud.model.JuBaoVo
import com.open.party.cloud.view.home.juBao.fragment.JuBaoMyInfoFragment
import com.open.party.cloud.view.home.juBao.fragment.JuBaoOtherInfoFragment
import com.open.party.cloud.viewModel.BizViewModel
import com.sinothk.android.utils.XUtils
import kotlinx.android.synthetic.main.activity_12380_ju_bao_shi_ming.*
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

class JuBao12380JuBaoShiMingActivity : AppEventBusTitleBaseActivity() {

    var myInfoFragment: JuBaoMyInfoFragment? = null
    var otherInfoFragment: JuBaoOtherInfoFragment? = null

    companion object {
        val vo = JuBaoVo()
        var class1 = 0
        var class2 = 0
        var area = ""
    }

    var viewModel: BizViewModel? = null

    override fun getLayoutResId(): Int = R.layout.activity_12380_ju_bao_shi_ming

    private fun initTabView() {
        myInfoFragment = JuBaoMyInfoFragment()
        otherInfoFragment = JuBaoOtherInfoFragment()

        initTab(
            listOf(
                "举报人信息",
                "被举报人信息"
            ),
            listOf(
                myInfoFragment!!,
                otherInfoFragment!!
            )
        )
    }

    private fun initTab(titles: List<String>, fragments: List<Fragment>) {

        val fragmentPagerAdapter = object : FragmentPagerAdapter(supportFragmentManager, FragmentPagerAdapter.BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT) {
            override fun getCount(): Int {
                return fragments.size
            }

            override fun getItem(arg0: Int): Fragment {
                return fragments[arg0]
            }
        }

        viewPager.offscreenPageLimit = titles.size - 1
        viewPager.adapter = fragmentPagerAdapter
        //        pager.addOnPageChangeListener(this);

        scrollTab.setTitles(titles)
//        scrollTab.setNumber(1, "9", View.VISIBLE)//设置数字红点
        scrollTab.setViewPager(viewPager)
        scrollTab.setOnTabListener { index, _ -> viewPager.setCurrentItem(index, true) }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        class1 = intent.getIntExtra("class1", 0)
        class2 = intent.getIntExtra("class2", 0)
        area = intent.getStringExtra("area") as String

        setViewTitle("实名举报", "提交") {
//            XUtils.toast().show("提交成功")
//            finish()

            submit()
        }

        viewModel = BizViewModel()
        initTabView()
    }

    private fun submit() {

        val vo = JuBaoVo()

        createOtherInfo(vo)
        createMyInfo(vo)


        showLoadingDialog("正在提交")
        viewModel!!.putJuBaoInfo(vo)
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public fun eventBusCallback(result: ResultInfo<JuBaoBean>) {
        if (result.eventType != "putJuBaoInfo") {
            return
        }

        hideLoadingDialog()

        when (result.code) {
            ResultInfo.SUCCESS -> {
                if (result.data != null) {
                    // 感谢您的支持，我们将尽快核实情况，祝您生活愉快
                    // "code":"1592288194662"
                    CommonDialog.Builder(this)
                        .setTitle("举报已提交")
                        .setCanceledOnTouchOutside(false)
                        .setMessage("举报编码：" + result.data.code + "\n\n感谢您的支持，我们将尽快核实情况，祝您生活愉快!")
                        .setPositiveButton("知道了") {
                            finish()
                        }.setNegativeButton("返回") {
                            finish()
                        }
                        .show()

                } else {
                    XUtils.toast().show("数据异常")
                }
            }
            ResultInfo.TOKEN_OVERDUE -> {
                XUtils.toast().show("Token过期，请重新登录")
            }
            else -> {
                XUtils.toast().show(result.msg)
            }
        }
    }

    private fun createMyInfo(vo: JuBaoVo) {

        val v01 = myInfoFragment!!.infoInput01Et().text.toString()

        val v02 = myInfoFragment!!.infoInput02Et().text.toString()
        val v03 = myInfoFragment!!.infoInput03Et().text.toString()
        val v04 = myInfoFragment!!.infoInput04Et().text.toString()
        val v05 = myInfoFragment!!.infoInput05Et().text.toString()
        val v06 = myInfoFragment!!.infoInput06Et().text.toString()
        val v07 = myInfoFragment!!.infoInput07Et().text.toString()
        val v08 = myInfoFragment!!.infoInput08Et().text.toString()
        val v09 = myInfoFragment!!.infoInput09Et().text.toString()
        val v10 = myInfoFragment!!.infoInput10Et().text.toString()
        val v11 = myInfoFragment!!.infoInput11Et().text.toString()

        vo.infoArea = v01
        vo.infoName = v02
        vo.infoSex = v03
        vo.infoIdNo = v04
        vo.infoUnit = v05
        vo.infoPost = v06
        vo.infoMobile = v07
        vo.infoAddress = v08
        vo.infoEmail = v09
        vo.infoContactNumber = v10
        vo.infoPostalCode = v11
    }

    private fun createOtherInfo(vo: JuBaoVo) {
        val v05 = otherInfoFragment!!.input05Et().text.toString()
        val v07 = otherInfoFragment!!.input07Et()
        val v08 = otherInfoFragment!!.input08Et()
        val v09 = otherInfoFragment!!.input09Et().text.toString()

        if (XUtils.string().isEmpty(area)) {
            XUtils.toast().show("所在地区不能为空")
            return
        }
        vo.area = area

        if (XUtils.string().isEmpty(v05)) {
            XUtils.toast().show("请填写所在单位")
            return
        }
        vo.unit = v05

        if (XUtils.string().isEmpty(v07)) {
            XUtils.toast().show("请填写级别")
            return
        }
        vo.level = v07

        if (XUtils.string().isEmpty(v08)) {
            XUtils.toast().show("请填写问题类型")
            return
        }
        vo.questionType = v08

        if (XUtils.string().isEmpty(v09)) {
            XUtils.toast().show("请填写主要问题")
            return
        }
        vo.mainQuestion = v09

        if (class1 == JuBao12380JuBaoClassActivity.JB_CLass_Ge_Ren) {
            vo.type = "user"

            val v02 = otherInfoFragment!!.input02Et().text.toString()
            if (XUtils.string().isEmpty(v02)) {
                XUtils.toast().show("请填写被举报人")
                return
            }
            vo.name = v02

            val v03 = otherInfoFragment!!.input03Et()
            if (XUtils.string().isEmpty(v03)) {
                XUtils.toast().show("请填写性别")
                return
            }
            vo.sex = v03

            val v04 = otherInfoFragment!!.input04Et().text.toString()
            if (XUtils.string().isEmpty(v04)) {
                XUtils.toast().show("请填写政治面貌")
                return
            }
            vo.politicalOutlook = v04

            val v06 = otherInfoFragment!!.input06Et().text.toString()
            if (XUtils.string().isEmpty(v06)) {
                XUtils.toast().show("请填写所任职务")
                return
            }
            vo.post = v06

        } else {
            vo.type = "unit"
        }
    }
}